package com.cl.reptile.controller;

import org.springframework.stereotype.Controller;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author chenlong
 * @create 2021-07-24 23:07
 */
@Controller
public class ResouresDown {
    public static void main(String[] args) throws Exception {
        String url = "https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.72/bin/apache-tomcat-8.5.72-windows-x64.zip";
        //BufferedInputStream bis = null;
        //BufferedOutputStream bfos = null;
        FileOutputStream fos = null;
        InputStream is = null;
        try {
            URL resoureUrl = new URL(url);
            URLConnection urlConnection = resoureUrl.openConnection();
            Thread.sleep(3000);
            long resourceSize = urlConnection.getContentLengthLong();

            is = urlConnection.getInputStream();
            //bis = new BufferedInputStream(is);

            fos = new FileOutputStream(new File("D:\\apache-tomcat-8.5.72-windows-x64.zip"));
            //bfos = new BufferedOutputStream(fileOutputStream);

            int i = 0;
            long currentDownSize = 0;
            byte[] byAry= new byte[1024];
            while (( i=is.read(byAry)) != -1){
                currentDownSize += i;
                fos.write(byAry,0,byAry.length);
                System.out.println("已下载："+ currentDownSize + "      总大小：" + resourceSize  + "百分比：  " + (currentDownSize/resourceSize)*100);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            fos.close();
            is.close();
        }
    }
}
